<template>
  <div class="mainpg">
    <div class="main_spac1" style="padding-bottom: 5px">
      <div class="main_white padding10">
        <div class="mainor_form">
          <el-form
            :inline="true"
            :model="form"
            class="demo-form-inline"
            ref="userListFrom"
            label-width="90px"
            id="form"
          >
            <el-form-item label="分组名称：" prop="title">
              <el-input
                v-model.trim="form.title"
                placeholder="分组名称"
                clearable
                class="wid220"
              ></el-input>
            </el-form-item>
            <MainFormBtn
              @reset="reset"
              @getSearchFor="getArticleList"
              :loading="listLoading"
              :showBtn="false"
            >
            </MainFormBtn>
          </el-form>
        </div>
        <div class="main-controls-wrap">
          <div>
            <el-button
              type="primary"
              @click="(addedFormVisible = true), (type = 0)"
              >新增分组</el-button
            >
          </div>
        </div>
        <div class="main_table">
          <el-table
            ref="multipleTable"
            v-loading="listLoading"
            :data="list.list.data"
            element-loading-text="加载中"
            fit
            highlight-current-row
            :height="borderHeight - 32"
            :header-cell-style="tableHeightStyle"
          >
            <el-table-column label="分组名称" prop="title" />
            <el-table-column label="用户数量">
              <template slot-scope="scope">
                {{ scope.row.members.length }}
              </template>
            </el-table-column>
            <el-table-column
              label="管理操作"
              align="center"
              fixed="right"
              width="400"
              class-name="main_hei"
            >
              <template slot-scope="scope">
                <el-link type="danger" @click="exportPrice(scope.row.id)">
                  导出报价
                </el-link>
                <router-link :to="'/user/group/shield/' + scope.row.id">
                  <el-link type="warning">屏蔽商品</el-link>
                </router-link>
                <el-link
                  type="primary"
                  @click="edit_assig(scope)"
                  >编辑</el-link
                >
                <el-link
                  type="danger"
                  @click="(id = scope.row.id), delet(scope.$index)"
                  >删除</el-link
                >
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="main_page boxdq">
          <div></div>
          <!-- 分页 -->
          <div class="main_page_r">
            <MainPagination
              :current-page="current.page"
              :page-sizes="[20, 30, 50, 100]"
              :page-size="limit"
              layout="total, sizes, prev, pager, next, jumper"
              :total="totalCount"
              background
              @size-change="changeLimit"
              @current-change="changePage"
            />
          </div>
        </div>
      </div>
    </div>
    <el-dialog
      :title="type == 1 ? '编辑分组' : '新增分组'"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      @close="close"
      :visible.sync="addedFormVisible"
      class="main_dialog dialog500"
    >
      <el-form
        :inline="false"
        :model="info"
        :rules="rules"
        class="demo-form-inline"
        ref="userListFrom"
        label-width="120px"
      >
        <el-form-item label="分组名称：" prop="title">
          <el-input
            v-model.trim="info.title"
            placeholder="分组名称"
            clearable
            class="wid300"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="(addedFormVisible = false), close()"
          >取 消</el-button
        >
        <el-button type="primary" @click="setupmine" v-repeatClick
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mixins } from "@/mixins/index.js";
import {
  group_index,
  group_add,
  group_edit,
  group_delete,
  group_confirmDelete,
  export_groubPrices,
  groupGoods_bindImportModel,
} from "@/api/user/userGroup";
import { groupGoodsBind_import } from "@/api/currency";
import rules from "@/rules/global/globalRole";
import { exportMethod } from "@/utils/index";
import { apiUrl, objKeySort, Md5 } from "@/utils/public";

export default {
  mixins: [mixins],
  components: {},
  data() {
    return {
      // 增加和编辑
      exportMethod,
      groupGoodsBind_import,
      info: {
        title: "",
      },
      type: 0,
      id: 0,
      addedFormVisible: false,
      // 搜索
      form: {
        title: "",
      },
      // 验证
      rules,
      // 分页
      current: {
        page: 1,
        per_page: 20,
      },
      limit: 20,
      totalCount: 1,
      // 列表数据
      list: {
        list: {
          data: [],
        },
      },
      listLoading: false,
    };
  },
  methods: {
    reset() {
      this.$refs["userListFrom"].resetFields();
      this.group_index();
    },
    startGetting() {
      this.getArticleList();
    },
    // 关闭清空表单
    close() {
      this.info = {
        title: "",
      };
      this.$refs["userListFrom"].clearValidate();
    },
    // 搜索
    getArticleList() {
      this.current.page = 1;
      this.group_index();
    },
    // 删除
    delet(index) {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, { id: this.id })) +
              apiUrl().appsecret
          ),
        },
        apiUrl().name,
        { id: this.id }
      );
      this.$confirm("是否删除分组?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          group_delete(data).then((res) => {
            if (res.errcode == 0) {
              this.$message({
                message: "删除成功",
                type: "success",
              });
              this.list.list.data.splice(index, 1);
            } else if (res.errcode == 40030) {
              this.$confirm("是否强制删除分组?", "提示", {
                confirmButtonText: "强制删除",
                cancelButtonText: "取消",
                type: "warning",
              })
                .then(() => {
                  group_confirmDelete(data).then((res) => {
                    if (res.errcode == 0) {
                      this.list.list.data.splice(index, 1);
                    }
                  });
                })
                .catch(() => {});
            } else {
              this.$message.error(res.errmsg);
            }
          });
        })
        .catch(() => {});
    },
    // 确认
    setupmine() {
      this.$refs["userListFrom"].validate((valid) => {
        if (valid) {
          this.type == 0 ? this.add() : this.edit();
        }
      });
    },
    // 编辑赋值
    edit_assig(scope) {
      this.addedFormVisible = true
      this.type = 1
      this.id = scope.row.id
      this.info = {
        title: this.list.list.data[scope.$index].title,
      };
    },
    // 新增保存
    add() {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, this.info)) +
              apiUrl().appsecret
          ),
        },
        apiUrl().name,
        this.info
      );
      group_add(data).then((res) => {
        this.addedFormVisible = false;
        if (res.errcode == 0) {
          this.$message({
            message: "新增成功",
            type: "success",
          });
          this.current.page = 1;
          this.group_index();
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    // 编辑保存
    edit() {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(
              Object.assign(apiUrl().name, this.info, { id: this.id })
            ) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        this.info,
        { id: this.id }
      );

      group_edit(data).then((res) => {
        this.addedFormVisible = false;
        if (res.errcode == 0) {
          this.$message({
            message: "编辑成功",
            type: "success",
          });
          this.group_index();
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    // 分页
    changeLimit(pageSize) {
      this.current.per_page = pageSize;
      this.group_index();
    },
    changePage(page) {
      this.current.page = page;
      this.group_index();
    },
    // 列表
    group_index() {
      this.listLoading = true;
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, this.current)) +
              apiUrl().appsecret
          ),
        },
        apiUrl().name,
        JSON.parse(JSON.stringify(this.form)),
        this.current
      );
      group_index(data).then((res) => {
        this.listLoading = false;
        if (res.errcode == 0) {
          this.list = res.data;
          this.totalCount = res.data.list.total;
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    // // 导入
    // importDialog(id) {
    //   this.import_params.group_id=id
    //   this.import_file_name = "";
    //   this.percent_gs = 0;
    //   this.percent = 0;
    //   this.percentShow = false;
    //   this.percent_list = [];
    //   this.importArrLength = 1;
    //   this.cancelDisabled = false;
    //   this.importbuVisible = true;
    // },
    //     // 下载模板
    // downTemplate() {
    //   let form = {
    //     user_type: this.import_form.user_type,
    //     goods_type: this.import_form.goods_type,
    //   };
    //   let data = Object.assign(
    //     {
    //       sign: Md5(
    //         objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
    //       ),
    //     },
    //     apiUrl().name,
    //     form
    //   );
    //   const notify = this.$notify.info({
    //     title: "开始导出...",
    //     type: "success",
    //     duration: 0,
    //   });
    //   groupGoods_bindImportModel(data).then((res) => {
    //     this.exportMethod(res, "分组屏蔽商品导入模板", notify);
    //   });
    // },
    // importerr(err, file, fileList) {
    //   this.$message.error("上传失败");
    // },
    // importSuccess(response, file, fileList) {
    //   if (response.errcode != 0) {
    //     this.$message.error(response.errmsg);
    //     return;
    //   }
    //   this.import_file_name = file.name;
    //   this.$message.success("上传成功");
    // },
    // handleUpload() {
    //   this.import_params.string =
    //     this.import_form.start_line_num + "," + this.import_form.end_line_num;
    //   this.import_params.sign= Md5(
    //         objKeySort(
    //           Object.assign(apiUrl().name, this.info, { id: this.id })
    //         ) + apiUrl().appsecret
    //       )
    //   this.import_params.timeStamp=Math.round(new Date().getTime()/1000)
    // },
    exportPrice(id) {
      console.log(id);

      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        { group_id: id }
      );
      const notify = this.$notify.info({
        title: "开始导出...",
        type: "success",
        duration: 0,
      });
      export_groubPrices(data).then((res) => {
        this.exportMethod(res, "导出报价", notify);
      });
    },
  },
  created() {
    // 列表
    this.getNumerical(160);
    this.group_index();
  },
};
</script>
<style lang="scss" scoped>
.mainor_form form {
  width: calc(100% - 180px);
}
.main_dialog .mainor_form form {
  width: calc(100% - 120px);
}

/deep/ .el-form-item--small.el-form-item {
  margin-bottom: 10px !important;
}
</style>
